package com.scalar.chiptrack.operations.wipflush.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.wipflush.forms.WipFlushSetupForm;
import com.scalar.chiptrack.operations.wipflush.dao.WipFlushDetailDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * User: Sujan
 * Date: Nov 3, 2005
 * Time: 2:27:33 PM
 */
public class WipFlushSetupLoadProcessStepDetailsCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        WipFlushSetupForm wipFlushSetupForm = (WipFlushSetupForm)form;
        HttpSession session = request.getSession();
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
        String userId = pUser.getUserId();

        ActionForward actionForward = mapping.findForward( "productFamilyDetails" );

        HashMap errors = (HashMap)request.getAttribute ( ERROR_MESSAGES );
        HashMap info   = ( HashMap )request.getAttribute( INFO_MESSAGES );
        DBTransaction dbTransaction = null;
        int baseProductId = wipFlushSetupForm.getBaseProductId();
        boolean success = false;
        int processStepId = 0;
        try
        {
            processStepId = Integer.parseInt ( request.getParameter ( "processStepId" ) );
        }catch ( NumberFormatException ignore ){}
        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            if ( wipFlushSetupForm.getSelectedProcessStepId() > 0 ) //PREVIOUSLY SELECTED PROCESS STEP, IF SELECTED SAVE INFORMATION
            {
                if( pUser.isActionAllowed( Functionality.WIP_FLUSH_SETUP, User.USER_ACTION_MODIFY ) )
                {
                    WipFlushDetailDAO.saveWipFlushSetup( dbTransaction, wipFlushSetupForm);
                    info.put( "Wip Flush setup saved successfully", null );
                }
            }
            wipFlushSetupForm.setSelectedProcessStepId( processStepId ); //CURRENT SELECTED PROCESS STEP
            LinkedHashMap productFamilyDetailsMap = WipFlushDetailDAO.loadProductFamilyDetails( dbTransaction , baseProductId );
            if ( productFamilyDetailsMap != null && productFamilyDetailsMap.size() > 0 )
            {
                request.setAttribute( "productFamilyDetailsMap", productFamilyDetailsMap );
            }
            else
            {
                System.out.println ( "====CouldNot find base product details map" );
                errors.put( "Could not find Base Product", null);
            }
            WipFlushDetailDAO.loadProcessStepBinDetails ( dbTransaction, wipFlushSetupForm );
            success = true;
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            errors.put( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not find Base Product details");
            errors.put( SessionKeys.ERROR_REPORT, e );
        }
        finally
        {
            if ( dbTransaction != null )
            {
                try
                {
                    if ( success )
                    {
                        dbTransaction.commit();
                    }
                    else
                    {
                        dbTransaction.rollback();
                    }
                }
                catch ( DBXException e )
                {
                    e.printStackTrace();
                    System.out.println ( "--DBX Exception while committing/rollbacking in WipFlush Save command" );
                }
            }
        }
        return actionForward;
    }
}
